﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lucky_bgy
{
    public partial class setting : Form
    {
        public setting()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void setting_Load(object sender, EventArgs e)
        {
            DBHelper helper = new DBHelper();

            //基本设置
            DataTable dt = helper.ExecuteDataTable("select * from sys_config where valid_flag=1", null);
            if (dt.Rows.Count > 0)
            {
                TabPage page = tabControl1.TabPages["baseInfo"];
                //foreach(TabPage page in tabControl1.TabPages){
                    foreach (Control ctl in page.Controls)
                    {
                        if (ctl is TextBox)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++){
                                if (dt.Rows[i]["config_name"].ToString() == ctl.Name)//查询条件
                                {
                                    ctl.Text = dt.Rows[i]["config_value"].ToString();
                                }
                            }
                        }
                        else if (ctl is CheckBox)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                if (dt.Rows[i]["config_name"].ToString() == ctl.Name)//查询条件
                                {
                                    if(dt.Rows[i]["config_value"].ToString().Equals("1")){
                                        ((CheckBox)ctl).Checked = true;
                                    }
                                    else
                                    {
                                        ((CheckBox)ctl).Checked = false;
                                    }
                                }
                            }
                        }
                    }
                //}
                
            }

            //参与人员
            string select_sql = "select tel,user_name from bs_user";		    //查询的SQL语句
            DataTable dtUser = helper.ExecuteDataTable(select_sql, null);	   //执行查询操作,结果存放在dt中

            dataGridView1.DataSource = dtUser;

            //奖项设置
            DataTable dtPrize = helper.ExecuteDataTable("select * from bs_prize order by seq_order", null);	   //执行查询操作,结果存放在dt中

            dataGridView2.DataSource = dtPrize;
            dataGridView2.Columns["ID"].Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DBHelper helper = new DBHelper();

            TabPage page = tabControl1.TabPages["baseInfo"];
            //foreach (TabPage page in tabControl1.TabPages)
            //{
                foreach (Control ctl in page.Controls)
                {
                    if (ctl is TextBox)
                    {
                        helper.ExecuteNonQuery("update sys_config set config_value='"+ ctl.Text.Trim()+"' where config_name='" + ctl.Name + "'", null);
                    }
                    else if (ctl is CheckBox)
                    {
                        if (((CheckBox)ctl).Checked)
                        {
                            helper.ExecuteNonQuery("update sys_config set config_value='1' where config_name='" + ctl.Name + "'", null);
                        }
                        else
                        {
                            helper.ExecuteNonQuery("update sys_config set config_value='0' where config_name='" + ctl.Name + "'", null);
                        }
                    }
                }
            //}

            ConfigHelper.refParam(); //刷新缓存

            MessageBox.Show("保存成功");

        }

        private void button2_Click(object sender, EventArgs e)
        {

            DialogResult dr = MessageBox.Show("确认清空抽奖参与者？", "", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                DBHelper helper = new DBHelper();
                int i = helper.ExecuteNonQuery("delete * from bs_user", null);

                if (i > 0)
                {
                    dataGridView1.DataSource = null;
                    MessageBox.Show("共删除" + i + "位参与者");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            
        }

        private void btn_import_Click(object sender, EventArgs e)
        {
            DataTable dt = FileHelper.ImportTxt();

            if (dt != null)
            {
                if (dt.DefaultView.ToTable(true, "tel").Rows.Count < dt.Rows.Count)
                {
                    MessageBox.Show("有重复的数据，请处理后再导入");
                    return;
                }

                dataGridView1.DataSource = dt;
            }

            DBHelper helper = new DBHelper();
            helper.ExecuteNonQuery("delete * from bs_user", null);

            helper.UpdateAccess(dt,"select tel,user_name from bs_user");
            MessageBox.Show("导入完成");

        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //DataGridView tempGdv = sender as DataGridView;//获取事件发送者
            //if (e.RowIndex > -1 && e.ColumnIndex > -1)//防止 Index 出错
            //{
            //    String tempStr = tempGdv.Rows[e.RowIndex].Cells["id"].Value.ToString();
            //    MessageBox.Show(tempStr);
            //}  
        }

        private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //DataGridView tempGdv = sender as DataGridView;//获取事件发送者
            //if (e.RowIndex > -1 && e.ColumnIndex > -1)//防止 Index 出错
            //{
            //    String tempStr = tempGdv.Rows[e.RowIndex].Cells["id"].Value.ToString();
            //    MessageBox.Show(tempStr);
            //}  
            btn_save_prize.Enabled = true;
        }

        private void btn_save_prize_Click(object sender, EventArgs e)
        {
            DataTable dt = dataGridView2.DataSource as DataTable;
            DBHelper helper = new DBHelper();
            helper.ExecuteNonQuery("delete * from bs_prize", null);
            helper.UpdateAccess(dt, "select * from bs_prize");
            btn_save_prize.Enabled = false;
            MessageBox.Show("保存成功");
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            int index = dataGridView2.CurrentRow.Index;
            if (index > -1)
            {
                String tempStr = dataGridView2.Rows[index].Cells["id"].Value.ToString();
                DBHelper helper = new DBHelper();
                helper.ExecuteNonQuery("delete from bs_prize where ID="+tempStr, null);
                //dataGridView2.Rows.RemoveAt(index);

                DataTable dtPrize = helper.ExecuteDataTable("select * from bs_prize order by seq_order", null);	   //执行查询操作,结果存放在dt中

                dataGridView2.DataSource = dtPrize;
                dataGridView2.Columns["ID"].Visible = false;

                MessageBox.Show("删除成功");
            }
        }
    }
}
